﻿//==============================================================================  
//Copyright (C) 2012-2015 9UN.ORG. All rights reserved. 
//GUID：22eec166-ae32-4a43-824c-566f78d0aafd
//CLR Version: 4.0.30319.18033
//Code Author：Kevin Wang
//Contact：Email(Admin@9un.org),QQ(265382 or 74344)
//Filename：ImageParse
//Namespace：LibTest
//Functions：ImageParse  
//Created by Kevin Wang at 2013/2/1 22:05:28 http://blog.9un.org
//============================================================================== 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace LibTest
{
    public class ImageWaterParser
    {
        /// <summary>
        /// 图片水印
        /// </summary>
        /// <param name="img">图片</param>
        /// <param name="filename">保存文件名</param>
        /// <param name="watermarkFilename">水印文件名</param>
        /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中  9=右下</param>
        /// <param name="quality">附加水印图片质量,0-100</param>
        /// <param name="watermarkTransparency">水印的透明度 1--10 10为不透明</param>
        public static void AddImageSignPic(Image img, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
        {
            var g = Graphics.FromImage(img);
            //设置高质量插值法
            g.InterpolationMode = InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = SmoothingMode.HighQuality;
            Image watermark = new Bitmap(watermarkFilename);
            if (watermark.Height >= img.Height || watermark.Width >= img.Width)
            {
                return;
            }
            var imageAttributes = new ImageAttributes();
            var colorMap = new ColorMap
                               {
                                   OldColor = Color.FromArgb(255, 0, 255, 0),
                                   NewColor = Color.FromArgb(0, 0, 0, 0)
                               };
            ColorMap[] remapTable = { colorMap };
            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
            var transparency = 0.5F;
            if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
            {
                transparency = (watermarkTransparency / 10.0F);
            }
            float[][] colorMatrixElements = {
                                                new[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
                                                new[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
                                                new[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
                                                new[] {0.0f,  0.0f,  0.0f,  transparency, 0.0f},
                                                new[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
                                            };
            var colorMatrix = new ColorMatrix(colorMatrixElements);
            imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            var xpos = 0;
            var ypos = 0;
            switch (watermarkStatus)
            {
                case 1:
                    xpos = (int)(img.Width * (float).01);
                    ypos = (int)(img.Height * (float).01);
                    break;
                case 2:
                    xpos = (int)((img.Width * (float).50) - ((float)watermark.Width / 2));
                    ypos = (int)(img.Height * (float).01);
                    break;
                case 3:
                    xpos = (int)((img.Width * (float).99) - watermark.Width);
                    ypos = (int)(img.Height * (float).01);
                    break;
                case 4:
                    xpos = (int)(img.Width * (float).01);
                    ypos = (int)((img.Height * (float).50) - ((float)watermark.Height / 2));
                    break;
                case 5:
                    xpos = (int)((img.Width * (float).50) - ((float)watermark.Width / 2));
                    ypos = (int)((img.Height * (float).50) - ((float)watermark.Height / 2));
                    break;
                case 6:
                    xpos = (int)((img.Width * (float).99) - watermark.Width);
                    ypos = (int)((img.Height * (float).50) - ((float)watermark.Height / 2));
                    break;
                case 7:
                    xpos = (int)(img.Width * (float).01);
                    ypos = (int)((img.Height * (float).99) - watermark.Height);
                    break;
                case 8:
                    xpos = (int)((img.Width * (float).50) - ((float)watermark.Width / 2));
                    ypos = (int)((img.Height * (float).99) - watermark.Height);
                    break;
                case 9:
                    xpos = (int)((img.Width * (float).99) - watermark.Width);
                    ypos = (int)((img.Height * (float).99) - watermark.Height);
                    break;
            }
            g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
            //g.DrawImage(watermark, new System.Drawing.Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, System.Drawing.GraphicsUnit.Pixel);
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo ici = null;
            foreach (var codec in codecs)
            {
                if (codec.MimeType.IndexOf("jpeg", StringComparison.Ordinal) > -1)
                {
                    ici = codec;
                }
            }
            var encoderParams = new EncoderParameters();
            var qualityParam = new long[1];
            if (quality < 0 || quality > 100)
            {
                quality = 80;
            }
            qualityParam[0] = quality;
            var encoderParam = new EncoderParameter(Encoder.Quality, qualityParam);
            encoderParams.Param[0] = encoderParam;
            if (ici != null)
            {
                img.Save(filename, ici, encoderParams);
            }
            else
            {
                img.Save(filename);
            }
            g.Dispose();
            img.Dispose();
            watermark.Dispose();
            imageAttributes.Dispose();
        }

        /// <summary>
        /// 文字水印
        /// </summary>
        /// <param name="img">图片</param>
        /// <param name="filename">保存文件名</param>
        /// <param name="watermarkText">水印文字</param>
        /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中  9=右下</param>
        /// <param name="quality">附加水印图片质量,0-100</param>
        /// <param name="fontname">字体</param>
        /// <param name="fontsize">字体大小</param>
        public static void AddImageSignText(Image img, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize)
        {
            var g = Graphics.FromImage(img);
            var drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
            var crSize = g.MeasureString(watermarkText, drawFont);
            float xpos = 0;
            float ypos = 0;
            switch (watermarkStatus)
            {
                case 1:
                    xpos = img.Width * (float).01;
                    ypos = img.Height * (float).01;
                    break;
                case 2:
                    xpos = (img.Width * (float).50) - (crSize.Width / 2);
                    ypos = img.Height * (float).01;
                    break;
                case 3:
                    xpos = (img.Width * (float).99) - crSize.Width;
                    ypos = img.Height * (float).01;
                    break;
                case 4:
                    xpos = img.Width * (float).01;
                    ypos = (img.Height * (float).50) - (crSize.Height / 2);
                    break;
                case 5:
                    xpos = (img.Width * (float).50) - (crSize.Width / 2);
                    ypos = (img.Height * (float).50) - (crSize.Height / 2);
                    break;
                case 6:
                    xpos = (img.Width * (float).99) - crSize.Width;
                    ypos = (img.Height * (float).50) - (crSize.Height / 2);
                    break;
                case 7:
                    xpos = img.Width * (float).01;
                    ypos = (img.Height * (float).99) - crSize.Height;
                    break;
                case 8:
                    xpos = (img.Width * (float).50) - (crSize.Width / 2);
                    ypos = (img.Height * (float).99) - crSize.Height;
                    break;
                case 9:
                    xpos = (img.Width * (float).99) - crSize.Width;
                    ypos = (img.Height * (float).99) - crSize.Height;
                    break;
            }
            g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);
            g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);
            var codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo ici = null;
            foreach (var codec in codecs)
            {
                if (codec.MimeType.IndexOf("jpeg", StringComparison.Ordinal) > -1)
                {
                    ici = codec;
                }
            }
            var encoderParams = new EncoderParameters();
            var qualityParam = new long[1];
            if (quality < 0 || quality > 100)
            {
                quality = 80;
            }
            qualityParam[0] = quality;
            var encoderParam = new EncoderParameter(Encoder.Quality, qualityParam);
            encoderParams.Param[0] = encoderParam;
            if (ici != null)
            {
                img.Save(filename, ici, encoderParams);
            }
            else
            {
                img.Save(filename);
            }
            g.Dispose();
            //bmp.Dispose();
            img.Dispose();
        }
    }
}
